package com.example.tms.controller;

import com.example.tms.bean.CarView;
import com.example.tms.bean.ReturnInfo;
import com.example.tms.mapper.CarMapper;
import com.example.tms.mapper.CarVMapper;
import com.example.tms.mapper.DriverMapper;
import com.example.tms.mapper.OilcardMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tms.annotation.*;

/**
 * 车辆管理
 */
@RestController
public class CarMController {

    private final CarVMapper carVMapper;
    private final CarMapper carMapper;
    private final DriverMapper driverMapper;
    private final OilcardMapper oilcardMapper;

    @Autowired
    public CarMController(CarVMapper carVMapper, CarMapper carMapper, DriverMapper driverMapper, OilcardMapper oilcardMapper){
        this.carVMapper = carVMapper;
        this.carMapper = carMapper;
        this.driverMapper = driverMapper;
        this.oilcardMapper = oilcardMapper;
    }

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    //查询车辆列表
    @RequiresPermissions("271")
    @RequestMapping(value = "carmgmt",method = RequestMethod.GET)
    public ReturnInfo getCarMgmt(@RequestParam(required = false, defaultValue = "") String keyname,
                                 @RequestParam(required = false, defaultValue = "") String keyvalue){
        List<CarView> result = carVMapper.findAll((Root<CarView> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder)->{
            List<Predicate> list = new ArrayList<>();
            switch (keyname){
                case "cph":
                    list.add(criteriaBuilder.like(root.get("cph").as(String.class), "%" + keyvalue + "%"));
                    break;
                case "jsyxm":
                    list.add(criteriaBuilder.like(root.get("jsyxm").as(String.class), "%" + keyvalue + "%"));
                    break;
                case "clzt":
                    list.add(criteriaBuilder.like(root.get("clzt").as(String.class), "%" + keyvalue + "%"));
                    break;
                case "name":
                    list.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + keyvalue + "%"));
                    break;
                case "ykh":
                    list.add(criteriaBuilder.like(root.get("ykh").as(String.class), "%" + keyvalue + "%"));
                    break;
                case "sffp":
                    list.add(criteriaBuilder.like(root.get("sffp").as(String.class), "%" + keyvalue + "%"));
                    break;
                default:
                    break;
            }
            list.add(criteriaBuilder.equal(root.get("sfcz").as(Boolean.class), true));
            Predicate[] p = new Predicate[list.size()];
            Predicate predicate = criteriaBuilder.and(list.toArray(p));
            criteriaQuery.where(predicate);
            criteriaQuery.orderBy(criteriaBuilder.desc(root.get("lasttime")));
            return criteriaQuery.getRestriction();
        });
        return ReturnInfo.info2(result);
    }

    //查询车辆信息
    @MonitorRequest
    @RequiresPermissions("271")
    @RequestMapping(value = "carmgmt/{clid}",method = RequestMethod.GET)
    public CarView getCarMgmtInfo(@PathVariable("clid") String clid){
        return carVMapper.findByClidAndSfcz(clid,true);
    }

    //车辆 添加司机 添加油卡 更新状态
    @MonitorRequest
    @RequiresPermissions("272")
    @RequestMapping(value = "carmgmt",method = RequestMethod.PUT)
    public List<Map<String,Object>> addCarMamt(@RequestBody CarView carView){
        List<Map<String,Object>>  list = new ArrayList<>();
        Map<String,Object>  map = new HashMap<>();
        if(carMapper.findOne(carView.getClid())==null){
            map.put("clid","1");
        }
        if(!carView.getSjid().equals("")&&driverMapper.findBySjid(carView.getSjid())==null){
            map.put("sjid","1");
        }
        if(!carView.getYkid().equals("")&&oilcardMapper.findByYkid(carView.getYkid())==null){
            map.put("ykid","1");
        }
        list.add(map);
        if(map.isEmpty()){
            if(carView.getSjid()!=null){
                carMapper.updateCarSj(carView.getClid(),carView.getSjid());
                carMapper.updateCarfp(carView.getClid(),carView.getSffp());
                driverMapper.updateDriverCar(carView.getClid(),carView.getSjid());
                logger.info("车辆"+carView.getClid()+" 添加司机"+carView.getSjid());
            }else {
                carMapper.updateBackCarfp(carView.getClid(),carView.getClzt(),carView.getSffp());
            }
            if(carView.getYkid()!=null){
                carMapper.updateCarYk(carView.getClid(),carView.getYkid());
                logger.info("车辆"+carView.getClid()+" 添加油卡"+carView.getYkid());
            }
            if(carView.getClzt()!=null){
                carMapper.updateCarZt(carView.getClid(),carView.getClzt());
                logger.info("车辆"+carView.getClid()+" 更新状态"+carView.getClzt());
            }
        }
        return list;
    }

}
